/**
* Created by wl
* 项目管理>持有中-详情页-项目详情-项目详情编辑信息
*/
<template>
    <div  :class="{'ca-box': $route.name.includes('add'), 'ca-m-t-28':  $route.name.includes('add'), 'ca-p-20': $route.name.includes('add')}">
        <div class="ca-title">
            <h3>项目详情</h3>
            <div class="el-right" v-show="showBtn">
                <el-button type="primary" @click="save" size="small">保存</el-button>
                <el-button @click="close" size="small">取消</el-button>
            </div>
        </div>
        <el-form ref="formDetail" label-width="150px" :model='itemHoldInfoEntity' :rules='rules'>
            <el-row>
                <el-col :span="11">
                    <el-form-item label="公司名称">
                        <el-input type="text" v-model="itemHoldInfoEntity.companyName" :maxlength="100">
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="11">
                    <el-form-item label="社会信用统一代码">
                        <el-input type="text" v-model="itemHoldInfoEntity.creditCode" :maxlength="100">
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="11">
                    <el-form-item label="公司成立时间">
                        <el-date-picker
                            v-model="itemHoldInfoEntity.registrationTime"
                            type="date"
                            placeholder="选择日期"
                            class="el-wid-100">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="11">
                    <el-form-item label="企业性质">
                        <el-select v-model="itemHoldInfoEntity.companyNature" placeholder="请选择" class="el-wid-100">
                            <el-option
                            v-for="item in dropDown.nature"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="11">
                    <el-form-item label="法人代表">
                        <el-input type="text" v-model="itemHoldInfoEntity.representative" :maxlength="100">
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="11">
                    <el-form-item label="注册资本（万元）" prop='registeredCapital'>
                        <el-input type="number" v-model="itemHoldInfoEntity.registeredCapital">
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <!-- <el-row>
                <el-col :span="11">
                    <el-form-item label="注册地址">
                        <el-input type="text" v-model="itemHoldInfoEntity.registeredAddress" :maxlength="100">
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="11">
                    <el-form-item label="办公地址">
                        <el-input type="text" v-model="itemHoldInfoEntity.officeAddress" :maxlength="100">
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row> -->
            <el-row>
                <el-col :span="11">
                    <el-form-item label="项目轮次">
                        <el-select v-model="itemHoldInfoEntity.prostatus" placeholder="请选择" class="el-wid-100">
                            <el-option
                            v-for="item in dropDown.prostatus"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="11">
                    <el-form-item label="证券代码">
                        <el-input type="text" v-model="itemHoldInfoEntity.securitiesCode" :maxlength="100">
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="22">
                    <csRegion :address='officeAddress' ref="officeAddress" :title="'办公地址'" :labelWidth="'150px'"></csRegion>
                    <csRegion :address='registeredAddress' ref="registeredAddress" :title="'注册地址'" :labelWidth="'150px'"></csRegion>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>
<script type="text/ecmascript-6">
    import {api} from '../../api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    export default {
        data() {
            var validateNumber = (rule, value, callback) => {
                var reg = /^\d{1,10}(\.\d{1,6})?$/;
                if (value && !reg.test(value)) {
                    callback(new Error('请输入的数字范围在小数点前十位,小数点后六位'));
                } else {
                    callback();
                }
            };
            return {
                registeredAddress: {
                    id: '',
                    businessId: '',
                    province: '',
                    city: '',
                    district: '',
                    businessType: 1,
                    registrationDetailed: ''
                },//注册地址
                officeAddress: {
                    id: '',
                    businessId: '',
                    province: '',
                    city: '',
                    district: '',
                    businessType: 2,
                    registrationDetailed: ''
                }, //办公地址
                itemHoldInfoEntity: { //项目详情
                    businessLicence: '', //营业执照
                    companyName: '', //公司名称
                    companyNature: '', //企业性质
                    creditCode: '', //社会信用统一代码
                    id: null, //id
                    itemId: null, //项目ID
                    officeAddress: '', //办公地址
                    prostatus: '', //轮次
                    registeredAddress: '', //注册地址
                    registeredCapital: 0, //注册资本
                    registrationTime: '', //注册时间(公司创立时间)
                    representative: '', //公司法人代表
                    securitiesCode: '', //证券代码
                },
                region: [], //注册地和办公地
                dropDown: {
                    nature: this.$store.state.user.dictionaryMap.nature, //企业性质
                    prostatus: this.$store.state.user.dictionaryMap.prostatus, //项目轮次
                },
                rules: {
                    registeredCapital: [
                        { validator: validateNumber, trigger: 'blur' }
                    ],
                }
            }
        },
        watch: {
            // 深度 watcher
            itemHoldInfoEntity: {
                handler: function (itemHoldInfoEntity) {
                    this.$emit('detailInfo', itemHoldInfoEntity);
                },
                deep: true
            },
            registeredAddress: {
                handler: function (registeredAddress) {
                    this.region = []
                    this.region.push(registeredAddress, this.officeAddress)
                    this.$emit('region', this.region);
                },
                deep: true
            },
            officeAddress: {
                handler: function (officeAddress) {
                    this.region = []
                    this.region.push(this.registeredAddress, officeAddress)
                    this.$emit('region', this.region);
                },
                deep: true
            },
        },
        props: {
            showBtn: {
                type: Boolean,
                default: function () {
                    return false;
                }
            },
            id: {
                type: String,
                default: function () {
                    return '';
                }
            },
            itemId: {
                type: String,
                default: function () {
                    return '';
                }
            },
        },
        mounted() {
            this.$emit('detailInfo', this.itemHoldInfoEntity);
            if (this.$route.params.itemId !== '-1' && !this.$route.name.includes('add')) {
                this.getInfo();
            } else if (this.$route.params.itemId !== '-1' && this.$route.name.includes('add')) {
                this.getPreInfo();
            }
        },
        methods: {
            // 取消
            close() {
                this.$emit('close')
            },
            // 获取持有中数据
            getInfo() {
                apiHttp(api.CHECK_INVESTED, {itemId: this.$route.params.itemId}).then(ret => {
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.itemHoldInfoEntity = ret.data.itemHoldInfoEntity
                        this.region = ret.data.region
                        //地区赋值
                        if (this.region && this.region.length !== 0 ) {
                            this.region.map((item)=> {
                                if (item.businessType === 1) { //注册地址
                                    this.registeredAddress = item
                                    //调用子组件的方法----初始化组件
                                    this.$refs.registeredAddress.inview(this.registeredAddress)
                                }
                                if (item.businessType === 2) {//办公地址
                                    this.officeAddress = item
                                    //调用子组件的方法----初始化组件
                                    this.$refs.officeAddress.inview(this.officeAddress)
                                }
                            })
                        }
                    }
                })
            },
            // 获取拟投资数据
            getPreInfo() {
                apiHttp(api.CHECK_PREDETAIL, {itemId: this.$route.params.itemId}).then(ret => {
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.itemHoldInfoEntity.companyNature = ret.data.companyNature // 企业性质
                        this.itemHoldInfoEntity.officeAddress = ret.data.officeAddress // 办公地址
                        this.itemHoldInfoEntity.prostatus = ret.data.prostatus // 项目轮次
                        this.itemHoldInfoEntity.registeredAddress = ret.data.registeredAddress // 注册地址
                        this.itemHoldInfoEntity.registeredCapital = ret. data.registeredCapital // 注册资本
                        this.itemHoldInfoEntity.registrationTime = ret.data.registrationTime // 成立时间
                        this.itemHoldInfoEntity.securitiesCode = ret.data.securityCode // 证券代码
                        this.itemHoldInfoEntity.id = ret.data.id
                        // this.itemHoldInfoEntity = ret.data.itemHoldInfoEntity
                    }
                })
            },
            // 保存
            save() {
                this.region = []
                //地区处理
                if (this.registeredAddress.province) {
                    this.region.push(this.registeredAddress)
                }
                if (this.officeAddress.province) {
                    this.region.push(this.officeAddress)
                }
                this.$refs.formDetail.validate((valid) => {
                    if (valid) {
                        this.itemHoldInfoEntity.officeAddress = this.officeAddress.registrationDetailed
                        this.itemHoldInfoEntity.registeredAddress = this.registeredAddress.registrationDetailed
                        let query = {
                            itemHoldInfoEntity: this.itemHoldInfoEntity,
                            region: this.region,
                            itemId: this.$route.params.itemId,
                        }
                        apiHttp(api.EDIT_INVESTED, query).then(ret => {
                            if (ret.resCode === STATUS_HTTP_SUCCESS) {
                                this.$message({
                                    message: '修改成功！',
                                    type: 'success',
                                    center: true
                                });
                                this.$emit('save')
                            }
                        })
                    } else {
                        return false;
                    }
                })
            },
        },
        components: {
            csRegion: require('__components/region'),//地区
        },
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.el-right {
    float: right;
}
.el-wid-100 {
    width: 100%;
}
.ca-title {
    margin-bottom: 10px;
}
.el-button--small, .el-button--small.is-round {
    margin-top: 1px;
}
</style>
